
#include "gmatlab.h"

// void g_boxed_free(GType, gpointer);
void mexFunction(int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[])
{
  GType gtype;
  gpointer value;

  // Check and convert arguments
  if (nrhs != 2) g_matlab_error("gmatlab:numArg", "g_boxed_free() takes 2 arguments");
  if (nlhs != 0) g_matlab_error("gmatlab:numRet", "g_boxed_free() has no return value");
  if (mxGetClassID(rhs[0]) != MX_GLONG) g_matlab_error("gmatlab:badType", "g_boxed_free: Arg 1 must be int, not %s", mxGetClassName(rhs[0]));
  if (mxGetClassID(rhs[1]) != MX_GPOINTER) g_matlab_error("gmatlab:badType", "g_boxed_free: Arg 2 must be int, not %s", mxGetClassName(rhs[0]));
  gtype = (GType)(((glong*)mxGetData(rhs[0]))[0]);
  value = (((gpointer*)mxGetData(rhs[1]))[0]);
  
  // Call function
  TRACE("g_boxed_free(%d, 0x%lx)", gtype, value);
  if (gtype == 0 || gtype == G_TYPE_NONE) g_free(value);
  else if (G_TYPE_IS_PARAM(gtype)) g_param_spec_unref(value);
  else g_boxed_free(gtype, value);
}
